package com.hp.service.chain.impl;

import com.hp.model.chain.OrderRequest;
import com.hp.service.chain.ValidationHandler;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


/**
 * @author hp
 * 第二步：库存校验
 */
@Component
@Order(2)
public class StockValidationHandler implements ValidationHandler {

    private ValidationHandler next;

    @Override
    public boolean validate(OrderRequest request) {

        // 假设商品ID 1-1000的ID是存在的
        if (request.getItemId() > 1000L && request.getQuantity() <= 10) {
            throw new RuntimeException("库存不足");
        }
        return next == null || next.validate(request);
    }

    @Override
    public void setNext(ValidationHandler next) {
        this.next = next;
    }
}